package com.yydm.serviceedu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yydm.commonutils.R;
import com.yydm.commonutils.ordervo.AutomobileVo;
import com.yydm.serviceedu.entity.Automobile;
import com.yydm.serviceedu.entity.Founder;
import com.yydm.serviceedu.service.AutomobileService;
import com.yydm.serviceedu.service.IFounderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yydm
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/serviceauto/founder")
//@CrossOrigin
public class FounderController {

    //    汽车
    @Autowired
    private AutomobileService automobileService;

    //    创始人
    @Autowired
    private IFounderService founderService;

    //    查询前8条热门汽车,查询前4名创始人
    @Cacheable(value = "name", key = "'index'")
    @GetMapping("/index")
    public R index() {
//        查询前8条热门汽车
        QueryWrapper<Automobile> wrapper = new QueryWrapper();
        wrapper.orderByDesc("product_sales")
                .last("limit 8");
        List<Automobile> automobileList = automobileService.list(wrapper);

//        查询前4名创始人
        QueryWrapper<Founder> wrapper1 = new QueryWrapper<>();
        wrapper1.orderByDesc("id")
                .last("limit 4");
        List<Founder> founderList = founderService.list(wrapper1);
        return R.ok().data("automobileList", automobileList).data("founderList", founderList);
    }

    @GetMapping("/getFounderName/{id}")
    public AutomobileVo getFounderName(@PathVariable Integer id) {
        Founder founder = founderService.getById(id);
//        把founder对象里面的值复制给ucentervo对象
        AutomobileVo automobileVo = new AutomobileVo();
        automobileVo.setFounderName(founder.getName());
        System.out.println(founder.getName());
        return automobileVo;
    }


}
